package org.ly2048.com.ly2048.terisme;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import org.ly2048.com.ly2048.R;

public class TerisMe extends Activity {

    private TerisView terisView;
    private ImageView img_back;
    public RefreshHandler refreshHandler;
    private float downX, downY;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设为无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        terisView = (TerisView) findViewById(R.id.terisme);
        img_back = (ImageView) findViewById(R.id.img_back);
        refreshHandler = new RefreshHandler(terisView);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                float dX = event.getX() - downX;
                float dY = event.getY() - downY;

                if (Math.abs(dX) < 50 && Math.abs(dY) < 50) {
                    return super.onTouchEvent(event);
                }
                Move(getOrientation(dX, dY));
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    private void Move(char orientation) {
        switch (orientation) {
            case 'l'://左移
                terisView.moveLeft();
                break;
            case 'r'://右移
                terisView.moveRight();
                break;
            case 't'://上
                terisView.turn();
                break;
            case 'd'://下移
                terisView.isMoveDown = true;
                break;
            default:
                break;
        }
    }

    private char getOrientation(float dX, float dY) {
        if (Math.abs(dX) > Math.abs(dY)) {
            if (dX > 0) {//右移
                return 'r';
            } else {//左移
                return 'l';
            }
        } else {
            if (dY > 0) {//下移
                return 'd';
            } else {//上
                return 't';
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (refreshHandler != null) {
            refreshHandler.pause();
            Log.v("teris", "pause");
        }
        // 游戏暂停
//         terisView.setMode(TerisView.STATE_MENU);
    }

    public void onResume() {
        super.onResume();
        if (refreshHandler != null) {
            refreshHandler.resume();
            refreshHandler.start();
            Log.v("teris", "resume");
        }
    }

    @Override
    protected void onDestroy() {
        terisView.toOver();
        super.onDestroy();
    }
}